<template>
  <a-form
    style="width: 300px"
    :model="formState"
    name="basic"
    :label-col="{ span: 8 }"
    :wrapper-col="{ span: 16 }"
    autocomplete="off"
    @finish="onFinish"
    @finishFailed="onFinishFailed"
  >
    <a-form-item
      label="Username"
      name="username"
      :rules="[{ required: true, message: 'Please input your username!' }]"
    >
      <a-input v-model:value="formState.username" />
    </a-form-item>

    <a-form-item
      label="Password"
      name="password"
      :rules="[{ required: true, message: 'Please input your password!' }]"
    >
      <a-input-password v-model:value="formState.password" />
    </a-form-item>
    <a-form-item
      label="机构ID"
      name="orgId"
      :rules="[{ required: true, message: '机构ID不能为空' }]"
    >
      <a-input v-model:value="formState.orgId" />
    </a-form-item>
    <a-form-item
      label="平台ID"
      name="platformId"
      :rules="[{ required: true, message: '平台ID不能为空' }]"
    >
      <a-input v-model:value="formState.platformId" />
    </a-form-item>
    <a-form-item name="captcha">
      <a-input v-model:value="formState.captcha" placeholder="验证码" />
      <img :src="captchaBase64" @click="onCaptcha" />
    </a-form-item>

    <a-form-item name="remember" :wrapper-col="{ offset: 8, span: 16 }">
      <a-checkbox v-model:checked="formState.remember">Remember me</a-checkbox>
    </a-form-item>

    <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
      <a-button type="primary" html-type="submit">Submit</a-button>
    </a-form-item>
  </a-form>
</template>
<script lang="ts" setup>
import { reactive, onMounted } from 'vue'
import { Login, getCaptcha } from '@/api/index'
// import { userStore } from '@/store/modules/user';
import { useRouter } from 'vue-router'
import store from '@/store'
const router = useRouter()

// const USER = userStore();

const captchaBase64 = ref('')

interface FormState {
  username: string
  password: string
  remember: boolean
  orgId: string
  platformId: string
  key: string
  captcha: string
}

const formState = reactive<FormState>({
  username: 'admin',
  password: 'admin',
  remember: true,
  orgId: store.userStore.orgId,
  platformId: store.userStore.platformId,
  key: '',
  captcha: ''
})

onMounted(() => {
  onCaptcha()
})

const onCaptcha = () => {
  getCaptcha().then((res) => {
    formState.key = res.data.key
    captchaBase64.value = res.data.image
  })
}

const onFinish = (values: any) => {
  // console.log('Success:', values)
  // store.userStore.setToken('123456')
  // router.push({
  //   path: '/home'
  // })
  const { username, password, remember, key, captcha, orgId, platformId } = formState
  Login({
    username,
    password,
    remember,
    key,
    captcha
  }).then((res) => {
    store.userStore.orgId = orgId
    store.userStore.platformId = platformId
    store.userStore.setToken(res.data.access_token)
    router.push({
      path: '/home'
    })
  })
}

const onFinishFailed = (errorInfo: any) => {
  console.log('Failed:', errorInfo)
}
</script>
